﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;


namespace GUI
{
    public partial class xeploai : Form
    {
        public xeploai()
        {
            InitializeComponent();
        }
        private void loadCombobox()
        {
            DataTable dtLop = new DataTable();
            dtLop = hocsinhBUS.danhsachlop();
            cmbLop.DataSource = dtLop;
            cmbLop.DisplayMember = "TenLop";
            cmbLop.ValueMember = "MaLop";
            cmbLop.SelectedIndex = -1;
        }
        private void xeploai_Load(object sender, EventArgs e)
        {
            loadCombobox();
        }

        private void cmbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbLop.SelectedIndex != -1)
            {
                lvwKetQua.Items.Clear();
                DataTable dtHocSinhLop = CustomBUS.TableQuery("select * from HOCSINH where MaLop='" + cmbLop.SelectedValue.ToString() + "'");
                DataTable dtHocLuc = hocsinhBUS.danhSachHocLuc();
                DataTable dtKetQua = hocsinhBUS.danhSachKetQua();
                ListViewItem lit;
                float f;
                float S;
                int dem;
                foreach (DataRow r in dtHocSinhLop.Rows)
                {
                    dem = 0;
                    S = 0;
                    f = 10;
                    lit = new ListViewItem();
                    lit.Text = r["MaHS"].ToString();
                    lit.SubItems.Add(r["HoTen"].ToString());
                    lit.SubItems.Add(cmbLop.Text);
                    foreach (DataRow r1 in dtKetQua.Rows)
                    {
                        if (r1["MaHS"].Equals(r["MaHS"]))
                        {
                            dem = dem + 1;
                            S = S + float.Parse(r1["DiemTB"].ToString());
                            if (float.Parse(r1["DiemTB"].ToString()) < f)
                                f = float.Parse(r1["DiemTB"].ToString());
                        }
                    }
                    S = S/dem;
                    S = (float)Math.Round(S, 1);
                    if (S.ToString() == "NaN")
                        lit.SubItems.Add("Chưa nhập điểm");
                    else
                        lit.SubItems.Add(S.ToString());
                    foreach (DataRow r2 in dtHocLuc.Rows)
                    {
                        if (S >= float.Parse(r2["DiemCanDuoi"].ToString()) && S <= float.Parse(r2["DiemCanTren"].ToString()) && f >= float.Parse(r2["DiemKhongChe"].ToString()))
                            lit.SubItems.Add(r2["TenHocLuc"].ToString());
                    }
                    lvwKetQua.Items.Add(lit);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult d = MessageBox.Show("Xác nhận thoát !", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
                this.Close();
        }
    }
}